$(function() {
	// 加载文章列表
	$(function() {
		$.ajax(baseURL + '/categories', {
			success: function(res) {
				const htmlStr = template('templateLi', {
					'article': res
				});
				$('.aside-nav .nav').html(htmlStr);
			},
			error: function(err) {
				console.log(err);
			}
		})
	})
	
	//加载轮播图
	$.ajax(baseURL+'/slides',{
		success:function(res){
			const htmlStr = template('templateSlideDiv', {'picSrc':res,'baseURL':baseURL});
			$('.content .swiper-container .swiper-wrapper').html(htmlStr);
			bindSwiper();
		},
		error:function(err){
			console.log(err);
		}
	})
	
	//加载热门推荐
	$.ajax(baseURL+'/posts/recommend',{
		success:function(res){
			const htmlStr = template('templateHotsLi', {'results':res,'baseURL':baseURL});
			$('.content .hots ul').html(htmlStr);
		},
		error:function(err){
			console.log(err);
		}
	})
	
	//加载最新发布
	$.ajax(baseURL+'/posts/lasted',{
		success:function(res){
			//格式化日期
			$.each(res, function(index, item) {
				res[index].author.createTime = item.author.createTime.slice(0,10);
			})
			
			const htmlStr = template('templateNewPublish', {'results':res,'baseURL':baseURL});
			$('.content .new').html(htmlStr);
		},
		error:function(err){
			console.log(err);
		}
	})
	
	//加载随机推荐
	$.ajax(baseURL+'/posts/random',{
		success:function(res){
			const htmlStr = template('templateRandomComment', {'results':res,'baseURL':baseURL});
			$('.aside .random').html(htmlStr);
		},
		error:function(err){
			console.log(err);
		}
	})
	
	//加载最新评论
	$.ajax(baseURL+'/comments/lasted',{
		success:function(res){
			//格式化日期
			$.each(res, function(index, item) {
				res[index].author.createTime = item.author.createTime.slice(0,10);
			})
			
			const htmlStr = template('templateNewestComment', {'results':res,'baseURL':baseURL});
			$('.aside .discuz').html(htmlStr);
		},
		error:function(err){
			console.log(err);
		}
	})
	
	
	//初始化轮播图
	function bindSwiper(){
		var mySwiper = new Swiper('.swiper-container', {
			loop: true,
			autoplay: {
				delay: 3000,
				stopOnLastSlide: false,
				disableOnInteraction: false, //用户操作swiper后是否禁止继续自动播放
			},
			pagination: {
				el: '.swiper-pagination',
			}
		});
		
		$('.pre-btn').click(function() {
			mySwiper.slidePrev();
		})
		
		$('.next-btn').click(function() {
			mySwiper.slideNext();
		})
	}
})
